<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Themnhanvienvaodonvi Model
 */
class VastModelThemnhanvienvaodonvi extends JModelForm {

    public function createItem($data) {
        // set the data into a query to update the record
        $donviid = JRequest::getInt('donviid');
        
        $db = $this->getDbo();        
        
        $query = $db->getQuery(true);
        
        $query->insert('#__hosocanbo');
        $query->set('hoten = ' . $db->quote($data['hoten']));
        $query->set('gioitinh = ' . (int)$data['gioitinh']);
        $query->set('ngaysinh = ' . $db->quote($data['ngaysinh']));
        $query->set('taikhoandientu = ' . $db->quote($data['taikhoandientu']));
        $query->set('sohieu = ' . $db->quote($data['sohieu']));
        $query->set('chungminhnhandan = ' . $db->quote($data['chungminhnhandan']));
        $query->set('ngaycap = ' . $db->quote($data['ngaycap']));
        $query->set('noicap = ' . $db->quote($data['noicap']));
        $query->set('masothue = ' . $db->quote($data['masothue']));
        $query->set('noisinh = ' . $db->quote($data['noisinh']));
        $query->set('nguyenquan = ' . $db->quote($data['nguyenquan']));
        $query->set('diachihientai = ' . $db->quote($data['diachihientai']));
        $query->set('dienthoai = ' . $db->quote($data['dienthoai']));
        $query->set('didong = ' . $db->quote($data['didong']));
         $query->set('thudientu = ' . $db->quote($data['thudientu']));
        $query->set('dantoc = ' . $db->quote($data['dantoc']));
        $query->set('tongiao = ' . $db->quote($data['tongiao']));
       
        $db->setQuery($query);
                
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            $query = $db->getQuery(true);
            $query->select('last_insert_id() AS id');
            $query->from('#__hosocanbo');
            
            $db->setQuery($query);
            
            $tmp = $db->loadObject();
            
            $id = (int)$tmp->id;
            
            $query = $db->getQuery(true);
            $query->insert('#__congtac');
            $query->set('hosocanbo_id = ' . (int)$id);
            $query->set('donvi_id = ' . (int)$donviid);
            $query->set('phong_id = 0');
            $query->set('lanhdao = 0');
            $query->set('chucdanh = ' . $db->quote($data['chucdanh']));
            $query->set('taivi = 1');
            $query->set('tinhtranglamviec_id = ' . (int)$data['tinhtranglamviec_id']);
            $query->set('batdau = ' . $db->quote($data['batdau']));
            $query->set('ketthuc = ' . $db->quote($data['ketthuc']));
            
            $db->setQuery($query);
                        
            if (!$db->query()) {
                JError::raiseError(500, $db->getErrorMsg());
                return false;
            }
            else
            {
                return true;                
            }    
        }
    }

    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_vast.themnhanvienvaodonvi', 'themnhanvienvaodonvi', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id')) {
            $id = $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else {
            $id = $jinput->get('id', 0);
        }

        return $form;
    }

    protected function loadFormData()
    {
        $data = array();
                        
        return $data;
    }
    
    public function getDonvi() 
    {
        $id = JRequest::getInt('donviid');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $this->donvi = $db->loadObject();
        
        return $this->donvi;
    }

    public function getKhoi() 
    {
        $id = JRequest::getInt('donviid');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__donvi as a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $donvi = $db->loadObject();
        
        if ($donvi !== NULL)
        {
            $khoiId = $donvi->khoi_id;
            $query = $db->getQuery(true);
            
            $query->select('a.*');
            $query->from('#__khoi as a');
            $query->where('a.id = ' . $khoiId);
            
            $db->setQuery($query);

            return $db->loadObject();                
        }
        
        return NULL;
    }

    public function getTonGiaoJSON()
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__tongiao AS a');

        $db->setQuery($query);
        $tmp = $db->loadObjectList();
        
        $count = count($tmp);
        
        $result = '';
        
        for ($i = 0;$i < $count - 1;$i++)
        {
            $row = $tmp[$i];
            $result .= '{"value": ' . (int)$row->id . ', "text": "' . $row->ten . '"},';    
        }
        
        $row = $tmp[$count - 1];
        $result .= '{"value": ' . (int)$row->id . ', "text": "' . $row->ten . '"}';
        
        return $result;        
    }
    
    public function getDanTocJSON()
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__dantoc AS a');

        $db->setQuery($query);
        $tmp = $db->loadObjectList();
        
        $count = count($tmp);
        
        $result = '';
        
        for ($i = 0;$i < $count - 1;$i++)
        {
            $row = $tmp[$i];
            $result .= '{"value": ' . (int)$row->id . ', "text": "' . $row->ten . '"},';    
        }
        
        $row = $tmp[$count - 1];
        $result .= '{"value": ' . (int)$row->id . ', "text": "' . $row->ten . '"}';
        
        return $result;        
    }
}